<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :before-close="reset"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="130px">
      <el-form-item  label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder=""></el-input>
        <p class="info"> </p>
      </el-form-item>
      <el-form-item  label="标识" prop="subscription">
        <el-input v-model="dataForm.subscription" placeholder=""></el-input>
        <p class="info">必须全英文 </p>
      </el-form-item>
      <el-form-item  label="备注" prop="note">
        <el-input v-model="dataForm.note" placeholder=""></el-input>
        <p class="info"> </p>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="reset()">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>


    <el-dialog
      width="30%"
      title="JSON参数编辑"
      :visible.sync="editPostJsonFlag"
      append-to-body>
      <div v-if="editPostJsonFlag">
        <vue-json-editor
          v-model="resultInfo"
          :showBtns="false"
          :mode="'code'"
          @json-change="onJsonChange"
          @json-save="onJsonSave"
          @has-error="onError"
        />
        <br>
      <el-button type="primary" @click="checkJson">确定</el-button>
    </div>
    </el-dialog>
  </el-dialog>
</template>

<script>
    import request from '@/utils/request'
 import vueJsonEditor from 'vue-json-editor'
  export default {
    data () {
      return {
        pageIdList:[],
        hasJsonFlag:false,
        // json数据
        resultInfo: {},
        textInfo:"",
        tableListData:[],
        visible: false,
        jsonType:'',
        editPostJsonFlag: false,
        dataForm: {
          name:"",
          note:"",
          subscription:""
        },
        dataRule: {
         
        }
      }
    },
    components:{
      vueJsonEditor
    },
    methods: {
      onJsonChange (value) {
        // 实时保存
        this.onJsonSave(value)
      },
      onError(value) {
        this.hasJsonFlag = false
      },
      //确定按钮 检查json
      checkJson(){
        this.dataForm[this.jsonType] = JSON.stringify(this.resultInfo)
        this.editPostJsonFlag = false
        return true
      },
      // 输入值的时候调用
      onJsonSave (value) {
        this.resultInfo = value
        this.hasJsonFlag = true
      },
      // input框获得焦点
      editJson (v,jsonObj) {
        if(jsonObj){
          this.resultInfo = JSON.parse(jsonObj) //json编辑器数据
        }else{
          this.resultInfo = {}
        }
        this.jsonType = v
        this.editPostJsonFlag = true
      },
      reset(){
        this.dataForm = {
          name:"",
          note:"",
          subscription:""
        }
        this.visible = false
      },
      changeUserInput(v){
        if(v.length==0){
          this.textInfo = '请先输入英文名称'
          return
        }else{
          this.textInfo = ''
        }
      },
      yzParmData(v){
        if(!v||v.length==0){
          this.textInfo = '请先输入英文名称'
          return
        }
        request({
          url: '/ht-datadevelop/table/manage/getTableNameCompliance',
          method: 'post',
          data:{tableName:v}
        }).then((res) => {
          if(res.data.success){
            this.textInfo = ""
          }else{
            this.textInfo = res.data.message
          }
        })
      },
      deleteSelect(i){
        this.tableListData.splice(i,1)
      },
      add(){
        this.tableListData.push({
          name:"",
          type:"varchar",
          length:"255",
          note:""
        })
      },
      init (row) {
        request({
          url:'/ht-datadevelop/token/manage/listToken',
          method: 'get',
          params:{}
        }).then(({data}) => {
          this.pageIdList  = data.page
        })
        if(row){
          this.textInfo = ""
          let obj = {...row}
          obj.jsonHeard = obj.jsonHeard==null?null:JSON.stringify(obj.jsonHeard)
          obj.jsonBody = obj.jsonBody==null?null:JSON.stringify(obj.jsonBody)
          obj.tokenId = obj.tokenId+''
          this.dataForm = JSON.parse(JSON.stringify(obj))
          this.tableListData = obj.tableList||[]
        }else{
          this.tableListData = []
        }
        this.visible = true
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            request({
              url: `/ht-datadevelop/realTime/api/save`,
              method: 'post',
              data:this.dataForm
            }).then((res) => {
              if (res.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.reset()
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(res.msg)
              }
            })
          }
        })
      }
    }
  }
</script>


<style lang="scss" scoped>
>>>.info{
    position: absolute;
    left: 0;
    top: 26px;
    font-size: 10px;
    width:100%;
    color: #ef9001;
  }
  .oneCls{
    >div{
      position: relative;
      >button{
        position: absolute;
        right: 0;
      }
    }
  }

  >>>.el-select{
    display: block !important;
  }
.editContent{
  border-radius: 10px;
  padding:10px;
  border:1px solid #eee;

  >div{
    max-height: 300px;
    overflow-y: scroll;
    .p2{
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    margin: 5px 0 3px 0;
    .el-input{
      width: 22%;
      margin:2px;
      box-sizing: border-box;
    }
    span{
      position: relative;
      top:5px;
      margin-left:16px;
      i{
        cursor: pointer;
      }
    }
  }
  }
  .p1{
    margin: 0;
    label{
      display: inline-block;
      width: 22%;
      text-align: center;
      background: #eee;
      font-weight: bold;
    }
  }
}
  .el-form{
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    .el-form-item{
      width:50%;
    }
  }
</style>
